Any Javascript or Coffeescript `map` function that transforms object *values*?
NickName:KajMagnus Ask DateTime:2012-06-11T10:36:40

Any Javascript or Coffeescript `map` function that transforms object *values*?

Is there no ubiquitous/standard Javascript or Coffeescript function that Transforms the values of an object/map/hash?

jQuery has $.map but it produces Arrays only.
Underscore has _.map but it also produces Arrays only.

To be clear, a function like this one is what I'm looking for. (This example is written in Coffeescript not Javascript.)

# Transforms the values in a map. (Doesn't modify `obj` — returns a new map.)
# Example usage:
#   mapObjVals({ a: 'aa', b: 'bb'}, (key, value) -> value + '_!')
#   --> { a: 'aa_!', b: 'bb_!' }
mapObjVals = (obj, f) ->
  obj2 = {}
  for k, v of obj
    obj2[k] = f k, v
  obj2

Copyright Notice:Content Author:「KajMagnus」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/10974010/any-javascript-or-coffeescript-map-function-that-transforms-object-values

More about “Any Javascript or Coffeescript `map` function that transforms object *values*?” related questions

Any Javascript or Coffeescript `map` function that transforms object *values*?

Is there no ubiquitous/standard Javascript or Coffeescript function that Transforms the values of an object/map/hash? jQuery has $.map but it produces Arrays only. Underscore has _.map but it also

Show Detail

Using Map in Coffeescript

I'm not sure what kind of map functionality javascript/coffeescript has, but suppose I convert a Java map to an appropriate format, can i use coffeescript to do something like thekey.getValue() ...

Show Detail

Google Maps global object; js generated by coffeescript not working

What is the global object in a Google Maps app? I rewrote the js at https://developers.google.com/maps/documentation/javascript/examples/map-geolocation in coffeescript, which generated the follo...

Show Detail

CoffeeScript map object to class instance

Is there any way to map object received from server as a JSON to class instance in CoffeeScript? I have a lot of cases where my JSON is: { id: '123' name: 'wojtek' age: 24 } and my c

Show Detail

What does the .map file do in coffeescript?

When I am coding in CoffeeScript, my IDE automatically compiles it to JavaScript. It however also creates a sameName.map file with it. On closer inspection of the map file, it just a lot of random

Show Detail

Searching object with recursive function in CoffeeScript

This is my first post on StackOverflow. Greetings! I am an absolute beginner programmer learning JavaScript and CoffeeScript. I am writing a recursive function to search through an object simply...

Show Detail

Calling JavaScript function from CoffeeScript file

I need to use a JavaScript library in my CoffeeScript application. Since I am not familiar with both languages I try something simple. My coffeescript file: empty = require('models/empty') c...

Show Detail

Extending Javascript objects with CoffeeScript

I want to add the ability to extend javascript objects by adding a method to the prototype. The method will receive one or more other objects and will add all of the key/values to this. This is wha...

Show Detail

CoffeeScript: Getter/Setter in Object Initializers

ECMAScript allows us to define getters or setters as following: [text/javascript] var object = { property: 7, get getable() { return this.property + 1; }, set setable(x) { this.property = x...

Show Detail

Coffeescript to javascript transpiling Inaccuracy

I don't know if this is IDE related, or purely about the transpiling process. I use Webstorm on a mac, and have node,npm and coffeescript installed. The IDE is using that to make the transpiling to

Show Detail